package com.mengxuegu.common.docs.config;

import com.mengxuegu.common.docs.properties.SwaggerProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.PostConstruct;

@Configuration
@EnableSwagger2
@EnableConfigurationProperties(SwaggerProperties.class)
@Slf4j
public class SwaggerServerConfig {
    private SwaggerProperties properties;

    public SwaggerServerConfig(SwaggerProperties swaggerProperties) {
        this.properties = swaggerProperties;
    }

    @PostConstruct
    private void checkProperties() {
        log.info("======================swagger2 Start======================");
        log.info(String.format("Swagger ServerName: %s Configuration--->Completed", properties.getName()));
        log.info("======================swagger2 End======================");

    }

    @Bean
    public Docket createRestApi() {
        BaseSwaggerConfig baseSwagger = new BaseSwaggerConfig();
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(baseSwagger.apiInfo(properties.getTitle(), properties.getDescription(), properties.getVersion(),
                        properties.getName(), properties.getUrl(), properties.getEmail())).select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build().globalOperationParameters(baseSwagger.setParameter()).securitySchemes(baseSwagger.securitySchemes())
                .securityContexts(baseSwagger.securityContexts());
    }
}